package com.hsytech.dijiebao.base;



import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;

import com.hsytech.dijiebao.R;


/**
 * Created by Administrator on 2015/8/27.
 */
public class SimpleDialogFrament extends DialogFragment {

	private static FragmentActivity fragmentActivity;
	private String msg = "";
	private DialogFragmentListener listener;
	private static SimpleDialogFrament mSimpleDialogFragment;

	private Button positive_btn, negative_btn;
	private TextView dialog_msg_tv;

	private SimpleDialogFrament() {

	}

	public static SimpleDialogFrament with(FragmentActivity fragmentActivity) {

		SimpleDialogFrament.fragmentActivity = fragmentActivity;

		if (mSimpleDialogFragment == null) {
			mSimpleDialogFragment = new SimpleDialogFrament();
		}

		return mSimpleDialogFragment;
	}

	public SimpleDialogFrament message(String msg) {

		if (fragmentActivity != null) {
			this.msg = msg;
		} else {

			throw new IllegalArgumentException("FragmentActivity must not be null.");
		}
		return mSimpleDialogFragment;
	}

	public SimpleDialogFrament message(int msgId) {

		if (fragmentActivity != null) {
			this.msg = fragmentActivity.getResources().getString(msgId);
		} else {

			throw new IllegalArgumentException("FragmentActivity must not be null.");
		}
		return mSimpleDialogFragment;
	}

	public SimpleDialogFrament attach(DialogFragmentListener listener) {

		if (fragmentActivity != null) {
			this.listener = listener;
		} else {

			throw new IllegalArgumentException("FragmentActivity must not be null.");
		}

		return mSimpleDialogFragment;
	}

	public void showDialog() {
		if (fragmentActivity != null) {
			mSimpleDialogFragment.show(fragmentActivity.getSupportFragmentManager(), msg);

		} else {

			throw new IllegalArgumentException("FragmentActivity must not be null.");
		}

	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
		View v = inflater.inflate(R.layout.dialog_fragment_simple, null);

		dialog_msg_tv = (TextView) v.findViewById(R.id.dialog_msg_tv);
		dialog_msg_tv.setText(msg);

		negative_btn = (Button) v.findViewById(R.id.negative_btn);
		negative_btn.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				listener.onNegative();
				mSimpleDialogFragment.dismiss();
			}
		});

		positive_btn = (Button) v.findViewById(R.id.positive_btn);
		positive_btn.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				listener.onPositive();
				mSimpleDialogFragment.dismiss();
			}
		});
		return v;
	}

}
